package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AdminUser;
import com.smfx.yxt.common.Constants;
import com.smfx.yxt.common.GlobalMap;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.SchoolCommentWeek;
import com.smfx.yxt.model.SysCommentTemplate;
import com.smfx.yxt.model.SysDictData;
import com.smfx.yxt.model.SysDictType;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.service.ISchoolCommentWeekService;
import com.smfx.yxt.service.ISysCommentTemplateService;
import com.smfx.yxt.service.ISysDictDataService;
import com.smfx.yxt.service.ISysDictTypeService;
import com.smfx.yxt.service.ITermInfoService;
import com.smfx.yxt.vo.SysCommentTemplateQueryVO;
import com.smfx.yxt.vo.SysDictDataQueryVO;

@RequestMapping("sys/dict")
@Controller
public class DictController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(DictController.class);

	@Autowired
	private ISysDictTypeService sysDictTypeService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	@Autowired
	private ISysCommentTemplateService sysCommentTemplateService;
	@Autowired
	private ISchoolCommentWeekService schoolCommentWeekService;
	@Autowired
	private ITermInfoService termInfoService;
	
	@RequestMapping(value = {"","/"})
	public String index() {
		return "sys/dictIndex";
	}
	
	@RequestMapping("commentTemplate")
	public String commentTemplate() {
		return "sys/commentTemplate";
	}
	
	@RequestMapping("weekTemplate")
	public String weekTemplate() {
		return "sys/weekTemplate";
	}
	
	@RequestMapping("dictType/list")
	@ResponseBody Object dictTypeList(HttpServletRequest request){
		try {
			SysDictType dictType = new SysDictType();
			AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
			dictType.setSchoolId(user.getTeacherInfo().getSchoolId());
			return sysDictTypeService.selectListForParam(dictType);
		} catch (Exception e) {
			logger.error("--dictTypeList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("dictData/page")
	@ResponseBody Object dictDataPage(SysDictDataQueryVO vo) {
		try {
			Page<SysDictData> page = new Page<SysDictData>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<SysDictData> list = sysDictDataService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--dictDataPage error={}", e);
		}
		return null;
	}
	
	@RequestMapping("weekTemplate/list")
	@ResponseBody Object weekTemplateList(SchoolCommentWeek record) {
		try {
			AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
			record.setSchoolId(user.getTeacherInfo().getSchoolId());
			return schoolCommentWeekService.selectListByParam(record);
		} catch (Exception e) {
			logger.error("--weekTemplateList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("dictType/addUpdate")
	@ResponseBody Object dictTypeAddUpdate(SysDictType dictType) {
		try {
			if(dictType.getId() != null) {
				dictType.setUpdateTime(new Date());
				sysDictTypeService.updateByPrimaryKeySelective(dictType);
			}else {
				sysDictTypeService.insertSelective(dictType);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--dictTypeAddUpdate, error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("dictData/addUpdate")
	@ResponseBody Object dictDataAddUpdate(SysDictData dictData) {
		try {
			if(dictData.getId() != null) {
				dictData.setUpdateTime(new Date());
				sysDictDataService.updateByPrimaryKeySelective(dictData);
			}else {
				sysDictDataService.insertSelective(dictData);
			}
			if(dictData.getDictTypeId().intValue() == 4) {
				GlobalMap.put(Constants.SYS_CONF, sysDictDataService.selectDictData("sys_conf"));
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--dictDataAddUpdate, error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("commentTemplate/page")
	@ResponseBody Object commentTemplatePage(SysCommentTemplateQueryVO vo) {
		try {
			Page<SysCommentTemplate> page = new Page<SysCommentTemplate>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<SysCommentTemplate> list = sysCommentTemplateService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--commentTemplatePage error={}", e);
		}
		return null;
	}
	
	@RequestMapping("commentTemplate/addUpdate")
	@ResponseBody Object commentTemplateAddUpdate(SysCommentTemplate record) {
		try {
			if(record.getId() != null) {
				record.setUpdateTime(new Date());
				sysCommentTemplateService.updateByPrimaryKeySelective(record);
			}else {
				sysCommentTemplateService.insertSelective(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--commentTemplateAddUpdate, error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("weekTemplate/addUpdate")
	@ResponseBody Object weekTemplateAddUpdate(SchoolCommentWeek record) {
		try {
			if(record.getId() != null) {
				record.setUpdateTime(new Date());
				schoolCommentWeekService.updateByPrimaryKeySelective(record);
			}else {
				AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
				record.setSchoolId(user.getTeacherInfo().getSchoolId());
				record.setTermId(getCurrentTerm().getId());
				schoolCommentWeekService.insertSelective(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--weekTemplateAddUpdate, error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("dictCode/exists")
	@ResponseBody int distCodeExists(String dictCode) {
		return sysDictTypeService.selectCountByDictCode(dictCode);
	}
	
	@RequestMapping("dictType/id")
	@ResponseBody Object dictTypeById(Integer id) {
		return sysDictTypeService.selectByPrimaryKey(id);
	}
	
	@RequestMapping("dictData/id")
	@ResponseBody Object dictDataById(Integer id) {
		return sysDictDataService.selectByPrimaryKey(id);
	}
	
	@RequestMapping("commentTemplate/id")
	@ResponseBody Object commentTemplateById(Integer id) {
		return sysCommentTemplateService.selectByPrimaryKey(id);
	}
	
	@RequestMapping("weekTemplate/id")
	@ResponseBody Object weekTemplateById(Integer id) {
		return schoolCommentWeekService.selectByPrimaryKey(id);
	}
	
	@RequestMapping("dictData/dictCode")
	@ResponseBody Object dictDataDictCode(String dictCode) {
		return sysDictDataService.selectListByDictCode(dictCode);
	}
	
	@RequestMapping("termList")
	@ResponseBody Object termList() {
		return termInfoService.selectList();
	}
}
